package javasec.contempt.views;

import javasec.contempt.data.SeedConnection;
import javasec.contempt.data.SeedConnector;
import javasec.contempt.visuals.VisualInput;

import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.*;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.jface.action.*;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.*;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;


public class VisualsView extends ViewPart
{
	private TableViewer viewer;
	private Action doubleClickAction;

	// the currently selected seed connection
	private SeedConnection selectedConnection=SeedConnector.getConnections()[0];
	/*
	 * The content provider class is responsible for
	 * providing objects to the view. It can wrap
	 * existing objects in adapters or simply return
	 * objects as-is. These objects may be sensitive
	 * to the current input of the view, or ignore
	 * it and always show the same content 
	 * (like Task List, for example).
	 */
	 
	class ViewContentProvider implements IStructuredContentProvider
	{
		public void inputChanged(Viewer v, Object oldInput, Object newInput)
		{
		}
		public void dispose()
		{
		}
		public Object[] getElements(Object parent)
		{
			// This is what you edit to add new Visuals
			//return new String[]{"LocalNetwork","Processing","Chat","Targets"};
			return new String[]{"Chat","Targets","RemoteNetwork"};
		}
	}
	class ViewLabelProvider extends LabelProvider implements ITableLabelProvider
	{
		public String getColumnText(Object obj, int index)
		{
			return getText(obj);
		}
		public Image getColumnImage(Object obj, int index)
		{
			return getImage(obj);
		}
		public Image getImage(Object obj)
		{
			return PlatformUI.getWorkbench().
				getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
		}
	}
	class NameSorter extends ViewerSorter
	{
	}

	/**
	 * The constructor.
	 */
	public VisualsView()
	{
	}

	/**
	 * This is a callback that will allow us
	 * to create the viewer and initialize it.
	 */
	public void createPartControl(Composite parent)
	{
		Composite composite=new Composite(parent, 0);
		composite.setLayout(new GridLayout(1,true));
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		Combo seedSelector = new Combo(composite, SWT.DROP_DOWN|SWT.READ_ONLY);
		for(SeedConnection connection:SeedConnector.connections)
		{
			seedSelector.add(connection.name);
		}
		seedSelector.select(0);
		seedSelector.setText("seed server");
		seedSelector.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
	    seedSelector.addSelectionListener(new SelectionListener()
	    {
	    	public void widgetSelected(SelectionEvent e)
	    	{
	    		String serverName=null;
	    		Object source=e.getSource();
	    		if(source instanceof Combo)
	    			serverName=((Combo)source).getText();
	    		else return;
	    		System.out.println("server name is "+serverName);
	    		for(SeedConnection connection:SeedConnector.connections)
	    		{
	    			if(connection.name.equals(serverName))
	    			{
	    				System.out.println("setting server to "+serverName);
	    				selectedConnection=connection;
	    				break;
	    			}
	    		}
			}

	    	public void widgetDefaultSelected(SelectionEvent e)
	    	{
			}
	    });
		
		viewer = new TableViewer(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		viewer.setContentProvider(new ViewContentProvider());
		viewer.setLabelProvider(new ViewLabelProvider());
		viewer.setSorter(new NameSorter());
		viewer.setInput(getViewSite());
		viewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
		
		// Create the help context id for the viewer's control
		PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "javasec.contempt.viewer");
		makeActions();
		hookContextMenu();
		hookDoubleClickAction();
		contributeToActionBars();
	}

	private void hookContextMenu()
	{
		MenuManager menuMgr = new MenuManager("#PopupMenu");
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener()
		{
			public void menuAboutToShow(IMenuManager manager)
			{
				VisualsView.this.fillContextMenu(manager);
			}
		});
		Menu menu = menuMgr.createContextMenu(viewer.getControl());
		viewer.getControl().setMenu(menu);
		getSite().registerContextMenu(menuMgr, viewer);
	}

	private void contributeToActionBars()
	{
		IActionBars bars = getViewSite().getActionBars();
		fillLocalPullDown(bars.getMenuManager());
		fillLocalToolBar(bars.getToolBarManager());
	}

	private void fillLocalPullDown(IMenuManager manager)
	{
	}

	private void fillContextMenu(IMenuManager manager)
	{
	}
	
	private void fillLocalToolBar(IToolBarManager manager)
	{
	}

	private void makeActions()
	{
		doubleClickAction = new Action()
		{
			public void run()
			{
				ISelection selection = viewer.getSelection();
				Object obj = ((IStructuredSelection)selection).getFirstElement();
				//showMessage("Double-click detected on "+obj.toString());
					
				IWorkbenchPage page= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				VisualInput input=new VisualInput("do it", selectedConnection);
				
				try
				{
					System.out.println("opening new editor");
					page.openEditor((IEditorInput)input,"javasec.contempt.visuals."+obj.toString()+"Visual");
				}
				catch(PartInitException e)
				{
					System.out.println(e);
				}
			}
		};
	}

	private void hookDoubleClickAction()
	{
		viewer.addDoubleClickListener(new IDoubleClickListener()
		{
			public void doubleClick(DoubleClickEvent event)
			{
				doubleClickAction.run();
			}
		});
	}
	private void showMessage(String message)
	{
		MessageDialog.openInformation(
			viewer.getControl().getShell(),
			"Visuals View",
			message);
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus()
	{
		viewer.getControl().setFocus();
	}
}